package com.jx.cashier.card.impl;

import android.content.Intent;
import android.nfc.NfcAdapter;

public class Coverter {

	// Hex help
	private final static byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1',
			(byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6',
			(byte) '7', (byte) '8', (byte) '9', (byte) 'A', (byte) 'B',
			(byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F' };

	/**
	 * convert a byte arrary to hex string
	 * 
	 * @param raw
	 *            byte arrary
	 * @param len
	 *            lenght of the arrary.
	 * @return hex string.
	 */
	public static String getHexString(byte[] raw, int len) {
		byte[] hex = new byte[2 * len];
		int index = 0;
		int pos = 0;

		for (byte b : raw) {
			if (pos >= len) {
				break;
			}
			pos++;
			int v = b & 0xFF;
			hex[index++] = HEX_CHAR_TABLE[v >>> 4];
			hex[index++] = HEX_CHAR_TABLE[v & 0xF];
		}

		return new String(hex);
	}

	public static String getUid(Intent intent){
		byte[] myNFCID = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
		return getHexString(myNFCID, myNFCID.length);
	}

	public static String cardNo8To10(String s) {

		if ( s == null || s.length() != 8){
			return "";
		}

		String s1 = s.substring(0, 2);
		String s2 = s.substring(2, 4);
		String s3 = s.substring(4, 6);
		String s4 = s.substring(6, 8);

		String x = s4 + s3 + s2 + s1;

		try {

			long l = Long.valueOf(x, 16);
			String xl = Long.toString(l);

			String[] xx = {"0000000000", "000000000", "00000000", "0000000",
					"000000", "00000", "0000", "000", "00", "0", ""};

			return xx[xl.length()] + xl;
		}
		catch (Exception e){
			return "";
		}
	}
}
